package com.zfzs.post.modules.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.model.vo.QueryAddAgentListVO;
import com.zfzs.post.modules.goods.domain.Goods;
import com.zfzs.post.modules.goods.dto.GoodsImgDTO;
import com.zfzs.post.modules.goods.service.GoodsService;
import com.zfzs.post.modules.goods.service.GoodsWebService;
import com.zfzs.post.modules.goods.service.dto.GoodsCriteria;
import com.zfzs.post.modules.goods.vo.GoodsImgVO;
import com.zfzs.post.modules.goods.vo.GoodsWebVO;
import com.zfzs.post.modules.post.domain.TerminalType;
import com.zfzs.post.modules.post.service.TerminalTypeService;
import com.zfzs.post.modules.system.service.UserService;
import com.zfzs.post.modules.system.service.dto.UserDTO;
import com.zfzs.post.utils.SecurityUtils;
import com.zfzs.post.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 后台商品管理
 *
 * @author ck
 * @date 2019/11/19 20:22
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class GoodsWebServiceImpl implements GoodsWebService {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private UserService userService;

    @Autowired
    private TerminalTypeService terminalTypeService;

    @Override
    public Map<String, Object> queryGoodsPageList(GoodsCriteria criteria, Integer num, Integer size) {
        Map<String, Object> map = new HashMap<>();
        //获取渠道ID
        Long id = SecurityUtils.getUserId();
        //管理员数据
        if (!id.equals(1L)) {
            criteria.setChannelId(id);
        }
        Page<Goods> page = goodsService.queryGoodsPageList(criteria, num, size);
        //重新封装数据
        List<Goods> goodsList = page.getContent();
        List<GoodsWebVO> list = goodsList.stream()
                .map(e -> {
                    List<String> infoStr = e.getInfoList();
                    List<GoodsImgVO> infoList = new ArrayList<>();
                    for (String s : infoStr) {
                        GoodsImgVO vo = new GoodsImgVO();
                        String name =  s.substring(s.lastIndexOf("/") + 1);
                        vo.setName(name);
                        vo.setUrl(s);
                        infoList.add(vo);
                    }
                    List<String> bannerStr = e.getBannerList();
                    List<GoodsImgVO> bannerList = new ArrayList<>();
                    for (String s : bannerStr) {
                        String name =  s.substring(s.lastIndexOf("/") + 1);
                        GoodsImgVO vo = new GoodsImgVO();
                        vo.setName(name);
                        vo.setUrl(s);
                        bannerList.add(vo);
                    }
                    GoodsWebVO goods = JSON.parseObject(JSON.toJSONString(e), GoodsWebVO.class);
                    goods.setInList(infoList);
                    goods.setBanList(bannerList);
                    //渠道名称
                    UserDTO user = userService.findById(e.getChannelId());
                    //机具名称
                    TerminalType terminalType = terminalTypeService.findById(e.getTerminalTypeId());
                    goods.setChannelName(user.getNickname());
                    goods.setTerminalTypeName(terminalType.getTerminalName());
                    return goods;
                }).collect(Collectors.toList());
        map.put("content", list);
        map.put("totalElements", page.getTotalElements());
        return map;
    }

    @Override
    public GoodsWebVO queryGoodsInfo(String id) {
        Goods goods = goodsService.findById(id);
        if (goods == null) {
            return null;
        } else {
            return GoodsVoToGoods(goods);
        }
    }

    @Override
    public void updateGoods(GoodsWebVO vo) {
        String id = vo.getId();
        if (StringUtils.isBlank(id)) {
            throw new BadRequestException("ID不能为空");
        }
        Goods goods = goodsService.findById(id);
        if (goods == null) {
            throw new BadRequestException("未查询到商品信息");
        }
        Goods g = GoodsToGoodsVo(vo);
        goodsService.save(g);
    }

    @Override
    public void saveGoods(GoodsWebVO goods) {
        Goods vo = GoodsToGoodsVo(goods);
        vo.setCreateTime(new Date());
        vo.setUpdateTime(new Date());
        goodsService.save(vo);
    }

    @Override
    public void delete(String id) {
        if (StringUtils.isBlank(id)) {
            throw new BadRequestException("ID不能为空");
        }
        Goods goods = goodsService.findById(id);
        if (goods == null) {
            throw new BadRequestException("未查询到商品信息");
        }
        goodsService.delete(id);
    }

    @Override
    public Goods GoodsToGoodsVo(GoodsWebVO vo) {
        Goods goods = JSON.parseObject(JSON.toJSONString(vo), Goods.class);
        List<String> bList = new ArrayList<>();
        List<String> iList = new ArrayList<>();
        List<GoodsImgVO> banList = vo.getBanList();
        if(banList != null){
            for (int i = 0; i < banList.size(); i++) {
                String imgUrl = banList.get(i).getUrl();
                bList.add(imgUrl);
            }
        }
        List<GoodsImgVO> inList = vo.getInList();
        if(inList != null){
            for (int i = 0; i < inList.size(); i++) {
                String imgUrl = banList.get(i).getUrl();
                iList.add(imgUrl);
            }
        }
        goods.setBannerList(bList);
        goods.setInfoList(iList);
        return goods;
    }

    @Override
    public GoodsWebVO GoodsVoToGoods(Goods e) {
        List<String> infoStr = e.getInfoList();
        List<GoodsImgVO> infoList = new ArrayList<>();
        for (String s : infoStr) {
            GoodsImgVO vo = new GoodsImgVO();
            String name =  s.substring(s.lastIndexOf("/") + 1);
            vo.setName(name);
            vo.setUrl(s);
            infoList.add(vo);
        }
        List<String> bannerStr = e.getBannerList();
        List<GoodsImgVO> bannerList = new ArrayList<>();
        for (String s : bannerStr) {
            GoodsImgVO vo = new GoodsImgVO();
            String name =  s.substring(s.lastIndexOf("/") + 1);
            vo.setName(name);
            vo.setUrl(s);
            bannerList.add(vo);
        }
        GoodsWebVO goods = JSON.parseObject(JSON.toJSONString(e), GoodsWebVO.class);
        goods.setInList(infoList);
        goods.setBanList(bannerList);
        //渠道名称
        UserDTO user = userService.findById(e.getChannelId());
        //机具名称
        TerminalType terminalType = terminalTypeService.findById(e.getTerminalTypeId());
        goods.setChannelName(user.getNickname());
        goods.setTerminalTypeName(terminalType.getTerminalName());
        return goods;
    }

    @Override
    public void updateImgList(GoodsImgDTO dto) {
        String id = dto.getId();
        String title = dto.getTitle();
        List<GoodsImgVO> list = dto.getList();
        if (StringUtils.isBlank(id)) {
            throw new BadRequestException("ID不能为空");
        }
        if (StringUtils.isBlank(title)) {
            throw new BadRequestException("类型不能为空");
        }
        Goods goods = goodsService.findById(id);
        if (goods == null) {
            throw new BadRequestException("未查询到商品信息");
        }
        //封装数据
        List<String> str = new ArrayList<>();
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                String url = list.get(i).getUrl();
                str.add(url);
            }
        }
        if (title.equals("商品详情图")) {
            goods.setInfoList(str);
        } else {
            goods.setBannerList(str);
        }
        goodsService.save(goods);
    }
}
